
import * as actions from './';
//   import BSPError from './../common/bsperror';
import HttpRequest from './../common/http';
const rootUrl = window.restConfig.rootUrl;

export const personalInfoInit = () => {
  return (dispatch, getState) => {
    dispatch({
      type: actions.PERSONAL_INFO_INIT
    })
  }
}

export const getPersonalInfo = (code, cb) => {
  return (dispatch) => {
    HttpRequest.get(rootUrl + '/personal/info/' + code)
      .then((ret) => {
        if (ret.code === 10000) {
          dispatch({
            type: actions.PERSONAL_INFO_CHANGE,
            data: {
              state: ret.data || {}
            }
          })
          if (cb) {
            cb();
          }
        }
      }).catch((err) => {
        dispatch({
          type: actions.PERSONAL_INFO_CHANGE,
          data: {
            error: {
              hasError: true
            }
          }
        })
        setTimeout( () => {
          dispatch({
            type: actions.PERSONAL_INFO_CHANGE,
            data: {
              error: {
                hasError: false
              }
            }
          })
        }, 3000)
      });
  }
}

export const stateChange = (name, value) => {
  return {
    type: actions.PERSONAL_INFO_CHANGE,
    data: {
      state: {
        [name]: value
      }
    }
  }
}